﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.facebook.graph.net {
    import flash.events.*;
    import flash.display.*;
    import flash.net.*;
    import flash.utils.*;
    import com.adobe.serialization.json.*;
    import com.facebook.graph.utils.*;
    import com.adobe.images.*;

    public class FacebookRequest {

        protected var _data:Object;
        protected var _success:Boolean;
        protected var _requestMethod:String;
        protected var urlRequest:URLRequest;
        protected var urlLoader:URLLoader;
        protected var _callback:Function;
        protected var _rawResult:String;
        protected var fileReference:FileReference;
        protected var _url:String;

        public function FacebookRequest(_arg1:String, _arg2:String="GET", _arg3:Function=null):void{
            _url = _arg1;
            _requestMethod = _arg2;
            _callback = _arg3;
        }
        protected function objectToURLVariables(_arg1:Object):URLVariables{
            var _local3:String;
            var _local2:URLVariables = new URLVariables();
            if (_arg1 == null){
                return (_local2);
            };
            for (_local3 in _arg1) {
                _local2[_local3] = _arg1[_local3];
            };
            return (_local2);
        }
        public function get success():Boolean{
            return (_success);
        }
        protected function isValueFile(_arg1:Object):Boolean{
            return ((((((((_arg1 is FileReference)) || ((_arg1 is Bitmap)))) || ((_arg1 is BitmapData)))) || ((_arg1 is ByteArray))));
        }
        protected function dispatchComplete():void{
            _callback(this);
            close();
        }
        protected function handelFileReferenceError(_arg1:ErrorEvent):void{
            _success = false;
            _data = _arg1;
            dispatchComplete();
        }
        protected function loadURLLoader():void{
            urlLoader = new URLLoader();
            urlLoader.addEventListener(Event.COMPLETE, handleURLLoaderComplete, false, 0, false);
            urlLoader.addEventListener(IOErrorEvent.IO_ERROR, handleURLLoaderIOError, false, 0, true);
            urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleURLLoaderSecurityError, false, 0, true);
            urlLoader.load(urlRequest);
        }
        public function call(_arg1:String, _arg2=null, _arg3:Function=null):void{
            var _local5:Object;
            var _local7:String;
            var _local8:ByteArray;
            if (_arg3 != null){
                _callback = _arg3;
            };
            var _local4:String = (_url + _arg1);
            urlRequest = new URLRequest(_local4);
            urlRequest.method = _requestMethod;
            if (_arg2 == null){
                loadURLLoader();
                return;
            };
            if (isValueFile(_arg2)){
                _local5 = _arg2;
            } else {
                if (_arg2 != null){
                    for (_local7 in _arg2) {
                        if (isValueFile(_arg2[_local7])){
                            _local5 = _arg2[_local7];
                            delete _arg2[_local7];
                            break;
                        };
                    };
                };
            };
            if (_local5 == null){
                urlRequest.data = objectToURLVariables(_arg2);
                loadURLLoader();
                return;
            };
            if ((_local5 is FileReference)){
                urlRequest.data = objectToURLVariables(_arg2);
                urlRequest.method = URLRequestMethod.POST;
                fileReference = (_local5 as FileReference);
                fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, handleFileReferenceData, false, 0, true);
                fileReference.addEventListener(IOErrorEvent.IO_ERROR, handelFileReferenceError, false, 0, false);
                fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handelFileReferenceError, false, 0, false);
                fileReference.upload(urlRequest);
                return;
            };
            var _local6:PostRequest = new PostRequest();
            for (_local7 in _arg2) {
                _local6.writePostData(_local7, _arg2[_local7]);
            };
            if ((_local5 is Bitmap)){
                _local5 = (_local5 as Bitmap).bitmapData;
            };
            if ((_local5 is ByteArray)){
                _local6.writeFileData(_arg2.fileName, (_local5 as ByteArray), _arg2.contentType);
            } else {
                if ((_local5 is BitmapData)){
                    _local8 = PNGEncoder.encode((_local5 as BitmapData));
                    _local6.writeFileData(_arg2.fileName, _local8, "image/png");
                };
            };
            _local6.close();
            urlRequest.contentType = ("multipart/form-data; boundary=" + _local6.boundary);
            urlRequest.data = _local6.getPostData();
            urlRequest.method = URLRequestMethod.POST;
            loadURLLoader();
        }
        public function get rawResult():String{
            return (_rawResult);
        }
        protected function handleURLLoaderIOError(_arg1:IOErrorEvent):void{
            var event = _arg1;
            _success = false;
            _rawResult = (event.target as URLLoader).data;
            if (_rawResult != ""){
                try {
                    _data = JSON.decode(_rawResult);
                } catch(e) {
                    _data = {type:"Exception", message:_rawResult};
                };
            } else {
                _data = event;
            };
            dispatchComplete();
        }
        public function get data():Object{
            return (_data);
        }
        public function toString():String{
            return ((urlRequest.url + ((urlRequest.data == null)) ? "" : ("?" + unescape(urlRequest.data.toString()))));
        }
        protected function handleURLLoaderSecurityError(_arg1:SecurityErrorEvent):void{
            var event = _arg1;
            _success = false;
            _rawResult = (event.target as URLLoader).data;
            try {
                _data = JSON.decode((event.target as URLLoader).data);
            } catch(e) {
                _data = event;
            };
            dispatchComplete();
        }
        protected function handleFileReferenceData(_arg1:DataEvent):void{
            handleDataLoad(_arg1.data);
        }
        public function close():void{
            if (urlLoader != null){
                urlLoader.removeEventListener(Event.COMPLETE, handleURLLoaderComplete);
                urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, handleURLLoaderIOError);
                urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, handleURLLoaderSecurityError);
                try {
                    urlLoader.close();
                } catch(e) {
                };
                urlLoader = null;
            };
            if (fileReference != null){
                fileReference.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, handleFileReferenceData);
                fileReference.removeEventListener(IOErrorEvent.IO_ERROR, handelFileReferenceError);
                fileReference.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, handelFileReferenceError);
                try {
                    fileReference.cancel();
                } catch(e) {
                };
                fileReference = null;
            };
        }
        protected function handleURLLoaderComplete(_arg1:Event):void{
            handleDataLoad(urlLoader.data);
        }
        protected function handleDataLoad(_arg1:Object, _arg2:Boolean=true):void{
            var result = _arg1;
            var dispatchCompleteEvent = _arg2;
            _rawResult = (result as String);
            _success = true;
            try {
                _data = JSON.decode(_rawResult);
            } catch(e) {
                _data = _rawResult;
                _success = false;
            };
            if (dispatchCompleteEvent){
                dispatchComplete();
            };
        }

    }
}//package com.facebook.graph.net 
